package com.yxym.poly.dynamic;

public class DynamticBinding {
    public static void main(String[] args) {
        A a = new B();//向上转型
        //编译类型为A，运行类型为B
        System.out.println(a.sum());//30
        System.out.println(a.sum1());//20
    }
}

class A {
    public int  i = 10;
    //动态绑定机制
    public int sum(){
        return getI() + 10 ;//20（返回的i值是子类的）+10=30
    }
    public int sum1(){
        return i + 10;//10（返回父类的i值）+10=20
    }
    public int getI(){
        return i;
    }
}

class B extends  A{
    public  int  i = 20 ;
//    public int sum(){
//        return i + 20;
//    }
    public int getI(){
        return i;//返回给父类的sum方法
    }
//    public int sum1(){
//        return i + 10;
//    }
}
